//
//  HRNetworkManage+Extension.swift
//  微博
//
//  Created by 众力金融 on 2017/1/11.
//  Copyright © 2017年 众力金融. All rights reserved.
//

import Foundation

//MARK: - 封装数据层
extension HRNetworkManager {
    
    /// 加载微博数据字典数组
    ///
    /// - Parameters: 字典数组
    ///   - since_id: 返回ID比since_id大的微博
    ///   - max_id: 返回ID小于或等于max_id的微博
    ///   - completion: 完成回调(list: 微博字典数组 / 是否成功)
    func statusList(since_id: Int64, max_id: Int64, completion: @escaping (_ list: [[String: AnyObject]]?, _ isSuccess: Bool)->()) {
       
        let url = "https://api.weibo.com/2/statuses/home_timeline.json"
       // let params = ["access_token":"2.00Rb2WWGCVGTQD1c0c3766b8pJcJ2D"]
        
        //Swift 中 Int可以转换成 Anyobject
        //提示服务器返回的字典数组,就是按照时间倒序排列的
        let params = ["since_id": since_id,"max_id": max_id > 0 ? max_id - 1 : 0]
    
        tokenRequest(URLString: url, parameters: params as [String : AnyObject]?) { (json, isSuccess) in
            
            let list = json["statuses"]  as? [[String: AnyObject]]
            completion(list, true)
            
        }

    }
    //返回微博的未读数量 - 定时刷新不需要返回成功和失败
    func unreadCount(completion: @escaping (_ count: Int)->()) {
        
        guard let uid = userAccount.uid else {
            return
        }
        
       let url = "https://rm.api.weibo.com/2/remind/unread_count.json"
        
       let params = ["uid": uid]
        
        tokenRequest(URLString: url, parameters: params as [String : AnyObject]?) { (json, isSuccess) in
            
            let dict = json as? [String: AnyObject]
            let count = dict?["status"] as? Int
            completion(count ?? 0)
            print(json)
        }
    }
}

//MARK: - 用户信息
extension HRNetworkManager {
    
    //加载用户信息
    func loadUserInfo(completion: @escaping (_ dict: [String: AnyObject])->()) {
        guard let uid = userAccount.uid else {
            return
        }
        
        let urlString = "https://api.weibo.com/2/users/show.json"
        let params = ["uid": uid]
        
        //发起网络请求
        tokenRequest(URLString: urlString, parameters: params as [String : AnyObject]?) { (json, isSuccess) in
            
            print(json)
            completion(json as? [String: AnyObject] ?? [:])
        }
    }
}

extension HRNetworkManager {
    //加载 AccessToken
    /**
     {
     "access_token" = "2.00Rb2WWGCVGTQDecfe355537ixsYVE";
     "expires_in" = 157679999;
     "remind_in" = 157679999;
     uid = 5977349275;
     }

     */
    func loadaccessToken(code: String, completion: @escaping (_ isSuccess: Bool)->()) {
        let urlString = "https://api.weibo.com/oauth2/access_token"
        let params = ["client_id": WBAppKey,
                      "client_secret": WBSecret,
                      "grant_type": "authorization_code",
                      "code": code,
                      "redirect_uri": WBRedirectURL]
        request(method: .POST, URLString: urlString, parameters: params as [String : AnyObject]?) { (json, isSccess) in
         
            print("json~~~AccessToken \(json)")
            //直接用字典设置 userAccount的属性
            self.userAccount.yy_modelSet(with: (json as? [AnyHashable : Any]) ?? [:])
            print(self.userAccount)
           
            //加载当前用户信息
            self.loadUserInfo(completion: { (dict) in
                print(dict)
                
                //使用用户信息设置用户信息
                self.userAccount.yy_modelSet(with: dict)
                //保存模型
                 self.userAccount.saveAccount()
                
                //完成回调 用户信息加载完成在回调
                completion(isSccess)
            })
            
        }
    }
}
